﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MadMediaManager.Core.Domain;
using NUnit.Framework;
using SharpTestsEx;

namespace MadMediaManager.DataAccess.Tests.Repositories
{
    [TestFixture]
    public class MovieIsoRepositoryTest : BaseRepositoryTest
    {
        [Test]
        public void Test_Properties_Mapped()
        {
            var iso = MovieIso.NewIso("path", "name", 5);
            iso.Rating = Rating.Three;
            iso.Year = 2005;
            iso.IsDeleted = true;

            iso.RowVersion.Should().Be.Null();

            DbContext.MovieIsos.Save(iso);
            FlushAndClear();

            iso.RowVersion.Should().Not.Be.Null();

            MovieIso found = DbContext.MovieIsos.Get(iso.EntityId);

            found.FileSize.Unit.Should().Be.EqualTo(iso.FileSize.Unit);
            found.FileSize.Value.Should().Be.EqualTo(iso.FileSize.Value);
            found.Name.Should().Be.EqualTo(iso.Name);
            found.Rating.Should().Be.EqualTo(iso.Rating);
            found.Year.Should().Be.EqualTo(iso.Year);
            found.IsoType.Should().Be.EqualTo(iso.IsoType);
            found.RowVersion.Should().Have.SameSequenceAs(iso.RowVersion);
            found.IsDeleted.Should().Be.EqualTo(iso.IsDeleted);
        }

        [Test]
        public void AddGenre_Genre_SavesToDatabase()
        {
            var iso = MovieIso.NewIso("path", "name", 5);
            iso.AddGenre("genre");

            DbContext.MovieIsos.Save(iso);
            FlushAndClear();

            var found = DbContext.MovieIsos.Get(iso.EntityId);
            found.Genres.Should().Contain("genre");
        }

        [Test]
        public void RemoveGenre_Genre_DeletesFromDatabase()
        {
            var iso = MovieIso.NewIso("path", "name", 5);
            iso.AddGenre("genre");

            DbContext.MovieIsos.Save(iso);
            DbContext.SubmitChanges();

            iso.RemoveGenre("genre");
            FlushAndClear();

            var found = DbContext.MovieIsos.Get(iso.EntityId);
            found.Genres.Should().Not.Contain("genre");
        }
    }
}
